package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Question;
import cn.wolfcode.trip.base.domain.QuestionComment;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.mapper.QuestionCommentMapper;
import cn.wolfcode.trip.base.mapper.QuestionMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IQuestionCommentService;
import cn.wolfcode.trip.base.service.IQuestionService;
import cn.wolfcode.trip.base.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class QuestionCommentServiceImpl implements IQuestionCommentService{
    @Autowired
    private QuestionCommentMapper commentMapper;
    @Override
    public PageInfo selectByQuestionId(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrderBy());
        List<QuestionComment> list = commentMapper.selectByQuestionId(qo);
        return new PageInfo(list);
    }

    @Override
    public void saveComment(QuestionComment entity) {
        User user = UserContext.getUserAttribute();
        if (entity.getId() == null) {
            entity.setUser(user);
            entity.setCreateTime(new Date());
            commentMapper.insert(entity);
        }
    }
}
